<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/17
 * Time: 9:39
 * 资讯
 */
namespace app\api\controller;
use think\Db;
class Information extends Base {

    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        header("Access-Control-Allow-Origin:*");
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 2111){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }
    }
    //资讯标签
    public function informationType(){
        $res = Db::table('up_information_type')->select();
        return  json(array('status'=>200,'msg'=>$res));
    }
    //搜索
    public function selectInformation(){
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $res  = array();
        if($keyword==''){
            return  json(array('status'=>200,'msg'=>$res));
        }else{
            $where['is_show'] = 1;
            $where['information_title'] = array('like',"%$keyword%");
            $res = Db::table('up_information')->where($where)
                ->field('information_id,is_admin,information_user_id,information_url,information_week,
                information_title,information_content,information_fa_time')
                ->order('information_fa_time desc')->limit($limit*$num,$num)->select();
            foreach ($res as $key=>&$vo){
                if($vo['is_admin']==1){//前台用户发布
                    $user_username = Db::table('up_u_user')->where('user_id',$vo['information_user_id'])->value('user_username');
                }elseif ($vo['is_admin']==2){//后台发布
                    $user_username = Db::table('up_admin')->where('id',$vo['information_user_id'])->value('username');
                }
                $vo['user_username']= $user_username;
            }
            return  json(array('status'=>200,'msg'=>$res));

        }
    }
    //发布资讯
    public function addInformation(){
        extract(input());
        $is_show_user = input('is_show_user'); // 是否匿名  1 不匿名   2 匿名
        $is_publish = input('is_publish'); // 是否发布  1 发布   2 草稿箱
        $title = input('param.title');
        $content = input('param.content');
        $title = $this->sensitive($title);
        $content = $this->sensitive($content); // 敏感词
        $information_url = input('param.information_url');
        $information_type_id = input('param.information_type_id');
        $data['information_title'] = $title;
        $data['information_content'] = $content;
        $data['information_url'] = $information_url;
        $data['information_type_id'] = $information_type_id;
        $data['information_user_id'] = $user_id;
        $data['is_admin'] = 1;
        $data['information_time'] = date('Y-m-d H:i:s',time());
        $data['information_fa_time'] = date('Y-m-d H:i:s',time());
        $data['information_week'] = date('Y-m-d',time());
        $data['is_show_user'] = $is_show_user;
        $data['is_publish'] = $is_publish;
        $img = explode(',',$information_url);
        $data['information_thumb'] =$img[0];
        $res = Db::table('up_information')->insertGetId($data);
        return  json(array('status'=>200,'msg'=>"成功"));
    }
    //本周资讯
    public function week(){
        $interest_ids = input('param.interest_ids');
        if($interest_ids != ''){
            $where['information_type_id'] = array('IN',$interest_ids);
        }
        $where['is_show'] = 1;
        $where['is_publish'] = 1;
        $where['information_selected'] = 1;
        $where['information_week'] = array('IN',$this->get_week());
        $res = Db::table('up_information')->where($where)
            ->field('information_id,is_admin,information_user_id,information_url,information_week,information_title,
            information_content, information_fa_time, is_show_user')
            ->order('information_fa_time desc')->limit(2)->select();
        foreach ($res as $key=>&$vo){
            if($vo['is_admin']==1){//前台用户发布
                $user_username = Db::table('up_u_user')->where('user_id',$vo['information_user_id'])->value('user_username');
            }elseif ($vo['is_admin']==2){//后台发布
                $user_username = Db::table('up_admin')->where('id',$vo['information_user_id'])->value('username');
            }
            $vo['user_username']= $user_username;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }
    //获取本周的日期
    public function get_week($time = '', $format='Y-m-d'){
        $time = $time != '' ? $time : time();
        //获取当前周几
        $week = date('w', $time);
        $date = [];
        for ($i=1; $i<=7; $i++){
            $date[$i] = date($format ,strtotime( '+' . $i-$week .' days', $time));
        }
        return $date;
    }

    //全部精选
    public function allSelected(){
        $interest_ids = input('param.interest_ids');
        if($interest_ids==''){
            $where['information_type'] = array('IN',$interest_ids);
        }
        $where['is_show'] = 1;
        $where['is_publish'] = 1;
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['information_selected'] = 1;
        $where['information_type'] = 1;
        $res = Db::table('up_information')->where($where)
            ->field('information_id,is_admin,information_user_id,information_url,information_week,
            information_title,information_content,information_fa_time, is_show_user')
            ->order('information_fa_time desc')->limit($limit*$num,$num)->select();
        foreach ($res as $key=>&$vo){
            if($vo['is_admin']==1){//前台用户发布
                $user_username = Db::table('up_u_user')->where('user_id',$vo['information_user_id'])->value('user_username');
            }elseif ($vo['is_admin']==2){//后台发布
                $user_username = Db::table('up_admin')->where('id',$vo['information_user_id'])->value('username');
            }
            $vo['user_username']= $user_username;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //推荐资讯
    public function recommend(){
        $interest_ids = input('param.interest_ids');
        if($interest_ids!=''){
            $where['information_type_id'] = array('IN',$interest_ids);
        }
        $where['is_show'] = 1;
        $where['is_publish'] = 1;
        $where['information_selected'] =2;
        $where['information_type'] = 1;
        $res = Db::table('up_information')->where($where)
            ->field('information_id,is_admin,information_user_id,information_url,information_week,information_title,
            information_content,information_fa_time, is_show_user')
            ->order('information_time desc')->limit(2)->select();
        foreach ($res as $key=>&$vo){
            if($vo['is_admin']==1){//前台用户发布
                $user_username = Db::table('up_u_user')->where('user_id',$vo['information_user_id'])->value('user_username');
            }elseif ($vo['is_admin']==2){//后台发布
                $user_username = Db::table('up_admin')->where('id',$vo['information_user_id'])->value('username');
            }
            $vo['user_username']= $user_username;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //全部推荐资讯
    public function allRecommend(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $interest_ids = input('param.interest_ids');
        if($interest_ids!=''){
            $where['information_type'] = array('IN',$interest_ids);
        }
        $where['is_show'] = 1;
        $where['is_publish'] = 1;
        $where['information_selected'] =2;
        $where['information_type'] = 1;
        $res = Db::table('up_information')->where($where)
            ->field('information_id,is_admin,information_user_id,information_url,information_week,
            information_title,information_content,information_fa_time, is_show_user')
            ->order('information_fa_time desc')->limit($limit*$num,$num)->select();
        foreach ($res as $key=>&$vo){
            if($vo['is_admin']==1){//前台用户发布
                $user_username = Db::table('up_u_user')->where('user_id',$vo['information_user_id'])->value('user_username');
            }elseif ($vo['is_admin']==2){//后台发布
                $user_username = Db::table('up_admin')->where('id',$vo['information_user_id'])->value('username');
            }
            $vo['user_username']= $user_username;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }
    //资讯详情
    public function selectedDetail(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        //查询详情
        $id = input('param.information_id');
        $whereMap1['information_id'] = $id;
        $res = Db::table('up_information')
            ->field('is_admin,information_user_id,information_url,information_week,
            information_title,information_content,information_fa_time, is_show_user')
            ->where($whereMap1)
            ->find();
        if(!$res){
            return json(['status' => 0, 'msg' => '数据不存在']);
        }
        // 获取富文本中图片
        preg_match_all('#src="([^"]+?)"#',$res['information_content'] , $m);
        if(is_array($m[1]) && !empty($m[1])){
            $res['content_url'] = $m[1][0];
        }else{
            $res['content_url'] = '';
        }
        $interest_ids = input('param.interest_ids');
        if($interest_ids!=''){
            $where2['information_type'] = array('IN',$interest_ids);
        }else{
            $where2 = 1;
        }
        $info3 = Db::table('up_information')
            ->field('is_admin,information_id,information_user_id,information_url,information_week,
            information_title,information_content,information_fa_time, is_show_user')
            ->where($where2)
            ->where('information_id', 'NEQ', $id)
            ->orderRaw('rand()')
            ->limit(3)
            ->select();
        foreach ($info3 as $key=>&$vo){
            if($vo['is_admin']==1){//前台用户发布
                $user_username1 = Db::table('up_u_user')->where('user_id',$res['information_user_id'])->value('user_username');
            }elseif ($vo['is_admin']==2){//后台发布
                $user_username1 = Db::table('up_admin')->where('id',$res['information_user_id'])->value('username');
            }
            $vo['user_username1'] = $user_username1;
        }
        if($res['is_admin']==1){//前台用户发布
            $user_username = Db::table('up_u_user')->where('user_id',$res['information_user_id'])->value('user_username');
            $user_image = Db::table('up_u_user')->where('user_id',$res['information_user_id'])->value('user_image');
        }elseif ($res['is_admin']==2){//后台发布
            $user_username = Db::table('up_admin')->where('id',$res['information_user_id'])->value('username');
            $user_image = Db::table('up_admin')->where('id',$res['information_user_id'])->value('portrait');
        }
        $res['user_username'] = $user_username;
        $res['user_image'] = $user_image;
        //查询有多少评论
        $where1['f_id'] = $id;
        $where1['type'] = 1;
        $num1 = Db::table('up_comment')->alias('a')
            ->where($where1)
            ->count();
        $res['comment_num'] = $num1;
        //查询评论数据
        $res1 = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where1)
            ->field('uuu.user_username,uuu.user_image,a.comment,a.comment_id,a.time,a.user_id, a.is_show_user')
            ->limit($limit*$num,$num)
            ->select();
        foreach ($res1 as $key=>&$vo){
            //评论有多少回复
            $count = Db::table('up_comment_reply')->where('f_id',$vo['comment_id'])->count();
            $vo['count'] = $count;
            //评论有多少点赞
            $map['give_f_id'] = $vo['comment_id'];
            $map['give_type'] = 8;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;

            $map1['give_type'] = 8;
            $map1['give_f_id'] = $vo['comment_id'];
            $map1['give_user_id'] = $user_id;
            $info = Db::table('up_comment_give')->where($map1)->find();
            if($info['give_id']!=''){//点赞过
                $vo['is_dz'] = 1;
            }else{
                $vo['is_dz'] = 2;
            }
        }
        //查询是否收藏这个资讯
        $whereMap['collection_f_id'] = $id;
        $whereMap['collection_type'] = 1;
        $whereMap['collection_user_id'] = $user_id;
        $collection = Db::table('up_collection')->where($whereMap)->find();
        if($collection['collection_id']!=''){
                $res['is_collection'] = 1;
        }else{
            $res['is_collection'] = 2;
        }
        $res['data'] = $res1;
        $res['data1'] = $info3;
        return  json(array('status'=>200,'msg'=>$res));
    }

    //评论
    public function comment(){
        extract(input());
        $is_show_user = input('is_show_user'); // 是否匿名   1 不匿名  2 匿名
        $content = input('param.content');
        $content = $this->sensitive($content); // 敏感词
        $video_id = input('param.information_id');
        $data['f_id'] = $video_id;
        $data['comment'] = $content;
        $data['time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $data['type'] = 1;
        $data['is_show_user'] = $is_show_user;
        //todo
        $data['f_user_id'] = input('param.other_user');
        $res = Db::table('up_comment')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //资讯评论回复
    public function informationAnswer(){
        extract(input());
        $comment_id = input('param.information_id');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
//        $comment_id = 15;
//        $user_id = 5;
        //查询详情
        $where['comment_id'] = $comment_id;
        $res = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where)
            ->field('uuu.user_username,uuu.user_image,a.comment,a.comment_id,a.time,a.user_id, a.is_show_user')
            ->find();
        //多少点赞
        $whereMap['give_type'] = 8;
        $whereMap['give_f_id'] = $comment_id;
        $count2 = Db::table('up_comment_give')->where($whereMap)->count();
        $res['count2'] = $count2;
        //自己是否点赞
        $whereMap1['give_type'] = 8;
        $whereMap1['give_f_id'] = $comment_id;
        $whereMap1['give_user_id'] = $user_id;
        $count3 = Db::table('up_comment_give')->where($whereMap1)->find();
        if($count3['give_id']!=''){
            $res['dz'] = 1;
        }else{
            $res['dz'] = 2;
        }
        //查询回复数据
        $map2['a.f_id'] = $comment_id;
        $info = Db::table('up_comment_reply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->field('a.*,uuu.user_username,uuu.user_image, a.is_show_user')
            ->where($map2)
            ->limit($num*$limit,$num)
            ->select();
        foreach ($info as $key=>&$vo){
            //评论回复有多少点赞
            $map['give_f_id'] = $vo['comment_reply_id'];
            $map['give_type'] = 3;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;
            //点赞
            $map1['give_type'] = 3;
            $map1['give_f_id'] = $vo['comment_reply_id'];
            $map1['give_user_id'] = $user_id;
            $info1 = Db::table('up_comment_give')->where($map1)->find();
            if($info1['give_id']!=''){//点赞过
                $vo['is_dz'] = 1;
            }else{
                $vo['is_dz'] = 2;
            }
        }
        //多少回复
        $num = Db::table('up_comment_reply')
            ->where('f_id',$comment_id)
            ->count();
        $res['num'] = $num;
        $res['info'] = $info;
        return  json(array('status'=>200,'msg'=>$res));
    }

    //评论回复
    public function answer(){
        extract(input());
        $is_show_user = input('is_show_user'); // 是否匿名   1 不匿名  2 匿名
        $content = input('param.content');
        $content = $this->sensitive($content); // 敏感词
        $comment_id = input('param.id');
        $zixun_id = input('zixun_id');
        $data['f_id'] = $comment_id; // 评论ID
        $data['f_f_id'] = $zixun_id; // 资讯ID
        $data['comment_reply_content'] = $content;
        $data['comment_time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $data['is_show_user'] = $is_show_user;
        $data['by_user_id'] = input('param.other_user');
        $res = Db::table('up_comment_reply')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //评论回复点赞
    public function Dz(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $type = input('param.type');
        if($type==1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 3;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }elseif ($type==2){
            $data['give_user_id'] = $user_id;
            $data['give_type'] =3;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }
      //评论点赞
    public function pingDz(){
        // type  1评论点赞   2评论点赞
        extract(input());
        $type = input('param.type');
        if($type==1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 8;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }elseif ($type==2){
            $data['give_user_id'] = $user_id;
            $data['give_type'] =8;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }

    //收藏
    public function collection(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $id = input('param.id');
            $data['collection_f_id'] = $id;
            $data['collection_user_id'] = $user_id;
            $data['collection_type']=1;
        $res1 = Db::table('up_collection')->where($data)->find();
            if($res1['collection_id']!=''){
                $res= Db::table('up_collection')->where($data)->delete();
            }else{
                $data['collection_type']=1;
                $res= Db::table('up_collection')->insertGetId($data);
            }
            if ($res) {
                return json(array('status' => 200, 'msg' => "收藏成功"));
            } else {
                return json(array('status' => 200, 'msg' => "收藏失败"));
            }
    }

}